package com.winit.issue.web;

import com.winit.common.base.bean.ExecuteResult;
import com.winit.common.base.web.BaseController;
import com.winit.issue.common.SequenceUtil;
import com.winit.issue.manager.IssueManager;
import com.winit.issue.model.Issue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(value = "issue", tags = {"issue"}, description = "issue")
@RestController
@RequestMapping("/api/v1/issue")
public class IssueController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(IssueController.class);

    @Autowired
    private IssueManager issueManager;

    private static class IssueModel{
        private List<Issue> issues;
//        public IssueModel(List<Issue> issues){
//            this.issues = issues;
//        }

        public List<Issue> getIssues() {
            return issues;
        }

        public void setIssues(List<Issue> issues) {
            this.issues = issues;
        }
    }

    @RequestMapping(value = "/saveIssues", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "创建/更新 多个issue")
    @ResponseBody
    public Map saveIssues(@RequestBody IssueModel issueModel){
        List<Issue> issues = issueModel.getIssues();
        issues = this.issueManager.saveIssues(issues);
        return this.getRtn(BaseController.REQUEST_SUCCESS, BaseController.REQUEST_SUCCESS_MSG, issues);
    }


    @RequestMapping(value = "/saveIssue", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "创建/更新issue")
    @ResponseBody
    public Map saveIssue(@RequestBody Issue issue){
        ExecuteResult<Issue> rs = null;
        if (StringUtils.isEmpty(issue.getId())){
            log.info("issue id,将执行创建");
            rs = this.issueManager.createIssue(issue);
        }else {
            log.info("issue id存在，将执行更新");
            rs = this.issueManager.updateIssue(issue);
        }

        Object data = rs.getData() == null ? null : rs.getData().getId();
        return this.getRtn(rs.getCode(), rs.getDesc(), data);
    }



    @RequestMapping(value = "/getIssueNumber", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取 issue number")
    @ResponseBody
    public Map getIssueNumber(){
        return this.getRtn(BaseController.REQUEST_SUCCESS, BaseController.REQUEST_SUCCESS_MSG, SequenceUtil.getIssueNumber());
    }





    @RequestMapping(value = "/getIssues", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "查询issue列表")
    @ResponseBody
    public Map getIssues(@ApiParam(value = "issueId")@RequestParam(value = "issueId", required = false)String issueId,
                         @ApiParam(value = "type")@RequestParam(value = "type", required = false)String type,
                         @ApiParam(value = "creatorAccount")@RequestParam(value = "creatorAccount", required = false)String creatorAccount,
                         @ApiParam(value = "toAccount")@RequestParam(value = "toAccount", required = false)String toAccount,
                         @ApiParam(value = "keyword")@RequestParam(value = "keyword", required = false)String keyword,
                         @ApiParam(value = "withComplete")@RequestParam(value = "withComplete", required = false, defaultValue = "0")String withComplete,
                         @ApiParam(value = "startDate")@RequestParam(value = "startDate", required = false)Long startDate){
        log.info("com.winit.issue.web.IssueController.getIssues start");
        if(StringUtils.isEmpty(keyword)){
            keyword = null;
        }
        List<Issue> issues = this.issueManager.getIssues(issueId, type, creatorAccount, toAccount, keyword, withComplete,startDate);
        log.info("com.winit.issue.web.IssueController.getIssues end");
        return this.getRtn(BaseController.REQUEST_SUCCESS, "success", issues);
    }


    @RequestMapping(value = "/getReqIssues", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "查询issue列表")
    @ResponseBody
    public Map getReqIssues(@ApiParam(value = "reqId")@RequestParam(value = "reqId", required = true)String reqId){
        List<Issue> issues = this.issueManager.getReqIssues(reqId);
        return this.getRtn(BaseController.REQUEST_SUCCESS, "success", issues);
    }

    @RequestMapping(value = "/deleteIssue", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "删除issue")
    @ResponseBody
    public Map deleteIssue(@ApiParam(value = "issueId")@RequestParam(value = "issueId", required = true)String issueId){
        ExecuteResult executeResult = this.issueManager.deleteIssue(issueId);
        return this.getRtn(executeResult.getCode(), executeResult.getDesc());
    }
}
